package czy.demo.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/* 登录控制器 */
@RestController
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    /* 登录接口 */
    @GetMapping("login")
    public String login(String userName,String password){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userName,password);

        try{
            /* shirl登录时会在配置的shiro.ini文件[users]标签下查找用户名密码 */
            subject.login(token);
            return "登录成功";
        }catch (AuthenticationException e){
            logger.error("登录失败",e);
            return "登录失败";
        }
    }

}
